package com.twu.biblioteca;

import java.util.NoSuchElementException;

public class BibliotecaApp {

    public static void main(String[] args) {
        BookList bookList = new BookList();
        MainMenu activeMenu = new MainMenu(bookList, System.in);

        System.out.println("+++++++ "+ new WelcomeMessage().getMessage() +" ++++++++1");
        while(true) {
            int userSelection = 0;

            try {
                userSelection = activeMenu.mainMenuLauncher();
            }
            catch (NoSuchElementException | NumberFormatException e) {
                System.out.println("your entry is not valid. Select a valid option!:");
            }

            switch (userSelection) {

                case 1:
                    System.out.println(new BookListFormatter(bookList.getAvailableBookList()).format());
                    break;

                case 2:
                    try {
                        int bookId = activeMenu.bookCheckout();
                        Book book = bookList.getBookById(bookId);
                        bookList.checkout(book);
                        System.out.println("Thank you! Enjoy the book");
                    } catch (NoSuchElementException e) {
                        System.out.println("That book is not available");
                    } catch (UnsupportedOperationException e) {
                        System.out.println("That book has been already checked out!");
                    }

                    break;

                case 3:
                    try {
                        int bookId = activeMenu.returnBook();
                        Book book = bookList.getBookById(bookId);
                        bookList.giveBack(book);
                        System.out.println("Thank you for returning the book");
                    } catch (IndexOutOfBoundsException e) {
                        System.out.println("That is not a valid book to return");
                    } catch (UnsupportedOperationException e) {
                        System.out.println("That book is currently in the library!" + e.getMessage());
                    }
                    break;

                case 9:
                    System.out.println("You are exiting Biblioteca. Good bye");
                    System.exit(0);
                    break;
            }

        }

    }
}
